package com.soda.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.soda.bean.Industry;
import com.soda.dao.IndustryDao;
import com.soda.dao.impl.IndustryDaoImpl;
import com.soda.service.IndustryService;
import com.soda.service.impl.IndustryServiceImpl;
import com.soda.util.PageInfo;
import com.soda.util.SelectIndustryInfo;

public class IndustryServlet extends HttpServlet {

	IndustryService is=new IndustryServiceImpl();
	IndustryDao idd=new IndustryDaoImpl();
	
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		PrintWriter out = response.getWriter();

		String param = request.getParameter("param");// 获得参数，由参数判断执行方法
		if (param != null) {
			// 查看
			if ("industry".equals(param)) {
				if(request.getParameter("ind")!=null){
					request.getSession().removeAttribute("sdi");
				}
				industry(request, response);
			
				return ;
			}
			//修改状态
			if ("updatestate".equals(param)) {
				updatestate(request, response);
			}
			
			// 删除
			if ("delete".equals(param)) {

				try {
					delete(request, response);
					return ;
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}

			// 编辑内容
			if ("edit".equals(param)) {

				edit(request, response);
				return ;
			}
			if ("update".equals(param)) {
				update(request, response);
				return ;
			}
			// 增加
			if ("add".equals(param)) {
				add(request, response);
				return ;
			}
			// 根据ID全查
						if ("showAllIndustry".equals(param)) {
							showAllIndustry(request, response);
							return ;
						}
			// 批量删除
			if ("alldelete".equals(param)) {
				alldelete(request, response);
				return ;
			}
			
			
		}
	 }

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		doGet(request, response);
	}
	

	// 增加
	protected void add(HttpServletRequest request, HttpServletResponse response) {
		String title = request.getParameter("titleName");
		int recommend = Integer.parseInt(request.getParameter("recommend"));
		String neirong = request.getParameter("neirong");
		HttpSession session = request.getSession();

	 
		Industry ids =new Industry();
		ids.setIndtitle(title);
		ids.setIndState(recommend);
		ids.setIndContent(neirong);
		int num = is.addIndustry(ids);
		if (num > 0) {

			try {
				industry(request, response);
			} catch (ServletException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

	}

	// 全查
	public void industry(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		 
		// 防止乱码

		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
 
		
		//PageInfo pi = new PageInfo();
		PageInfo pi = new PageInfo(1, 5, 0, 0, "IndustryServlet", "industry");
		// 从jsp中获取值
		SelectIndustryInfo sdi = new SelectIndustryInfo();

		String Title = request.getParameter("IndustryTitle");
		String updateTime = request.getParameter("updateTime");
		String state = request.getParameter("select2");
		
		
		HttpSession session = request.getSession();
		if (session.getAttribute("sdi") != null) {
			sdi = (SelectIndustryInfo) session.getAttribute("sdi");
		} else {
			sdi = new SelectIndustryInfo("", "", -1);
		}

		if (Title != null) { // 分页条件赋值
			sdi.setIndtitle(Title.length() == 0 ? "" : Title);

			sdi.setIndUpdateTime(updateTime ==null|| updateTime.length() == 0 ? "":updateTime);
			sdi.setIndState(state == null ||state.length() == 0  ? -1 : Integer.parseInt(state));
			request.getSession().setAttribute("sdi", sdi);
		} else {
 			sdi = (SelectIndustryInfo) request.getSession().getAttribute("sdi");
			 
		}

		int page = 1;
		pi.setPer(5);// 分页信息赋值
		int count=is.getIndustryCount(sdi);
		pi.setTotal(count);
		pi.setLastPage((count+pi.getPer()-1)/pi.getPer());
		
		String pages = request.getParameter("page");
 
		if (pages != null) {
			pi.setPage(Integer.parseInt(pages));
		}
		List<Industry>list = is.getIndustry(sdi, pi);
	 
		request.setAttribute("Industry", list);
		request.setAttribute("page", pi);
		session.setAttribute("st", state);
		request.getRequestDispatcher("houtai/pages/publicityManage/classManage/index.jsp").forward(request, response);

	}

	// 单个删除
	protected void delete(HttpServletRequest request,
			HttpServletResponse response) throws Exception, IOException {
		response.setContentType("text/html;charset=utf-8");
		int Id = Integer.parseInt(request.getParameter("id"));

		if (is.deleteById(Id) > 0) {
			 industry(request, response);
		}

	}

	// 批量删除
	protected void alldelete(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		String dellist = request.getParameter("id");
		 
		String []list=dellist.split(",");
 
		
		int num = is.delAllIndustry(list);
		PrintWriter out=response.getWriter();
		if (num > 0) {
			//industry(request, response);
	 
			}
	}

	// 根据ID查信息
	protected void edit(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		
		
		int id = Integer.parseInt(request.getParameter("editId"));
		Industry ind = is.showAllIndustry(id);
		request.setAttribute("Industry", ind);
		request.setAttribute("state", ind.getIndState());
		request.getRequestDispatcher("houtai/pages/publicityManage/classManage/edit.jsp").forward(request, response);

	}
	
	//
	protected void showAllIndustry(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");

		int id = Integer.parseInt(request.getParameter("Id"));
		Industry ind=is.showAllIndustry(id);
		request.setAttribute("Industry", ind);
		request.setAttribute("state", ind.getIndState());
		request.getRequestDispatcher("houtai/pages/publicityManage/classManage/lookImpl.jsp").forward(request, response);

	
	
	}
	//修改状态
		public void updatestate(HttpServletRequest request,
				HttpServletResponse response) {
			// TODO Auto-generated method stub
			
			try {
				int state=Integer.parseInt(request.getParameter("state"));
				int newId=Integer.parseInt(request.getParameter("newid"));
				//更新
				int n=is.updateStateNews(newId,state);
				PrintWriter out= response.getWriter();
				if(n>0){
					out.print("修改成功");
					//out.print("<script>location.href='NewsServlet?method='+'sel'</script>");
				}
				out.close();
			} catch (NumberFormatException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
		}
		
		
	// 修改信息
	public void update(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		int id = Integer.parseInt(request.getParameter("Id"));
		int recommend = Integer.parseInt(request.getParameter("recommend"));
		String neirong = request.getParameter("neirong");
		int n = is.updateIndustry(id, recommend, neirong);
		if (n > 0) {
			 industry(request, response);

		} else {
			System.out.println("shibai");
		}
	}}